
local PhysicsScene = class("PhysicsScene", function()
		return display.newScene("PhysicsScene", {physics = true,})
	end)

function PhysicsScene:ctor()
	print("this is the physics scene", self:getContentSize().width, self:getContentSize().height)
	self:getPhysicsWorld():setDebugDrawMask(cc.PhysicsWorld.DEBUGDRAW_ALL)
	-- 设置重力
    self:getPhysicsWorld():setGravity(cc.p(0, -100))

    -- 创建边界
    local size = display.size
    local body = cc.PhysicsBody:createEdgeBox(size, cc.PHYSICSBODY_MATERIAL_DEFAULT, 3)
    local edgeNode = display.newNode()
    edgeNode:move(display.cx, display.cy)
    edgeNode:setPhysicsBody(body)
    self:addChild(edgeNode)

    local function onContactBegin(contact)
    	local tag = contact:getShapeA():getBody():getNode():getTag()
    	print("contact begin tag", tag)
    	return true
    end

    local contactListener = cc.EventListenerPhysicsContact:create()
    contactListener:registerScriptHandler(onContactBegin, cc.Handler.EVENT_PHYSICS_CONTACT_BEGIN)
    local eventDispatcher = self:getEventDispatcher()
    eventDispatcher:addEventListenerWithFixedPriority(contactListener, 1)

    local function OnTouchOBOBegan(touch, event)
        self:addSprite(touch:getLocation())
        return true
    end

    local layer = display.newLayer(cc.c4b(128, 128, 128, 255))
    	:addTo(self)
    local listener = cc.EventListenerTouchOneByOne:create()
    listener:registerScriptHandler(OnTouchOBOBegan, cc.Handler.EVENT_TOUCH_BEGAN)
    eventDispatcher:addEventListenerWithSceneGraphPriority(listener, layer)
end

function PhysicsScene:addSprite(x, y)
	local sprite = display.newSprite("coin.png")
	local body = cc.PhysicsBody:createBox(sprite:getContentSize(), cc.PHYSICSBODY_MATERIAL_DEFAULT, cc.p(0, 0))
	body:setContactTestBitmask(0xFFFFFFFF)
	body:applyImpulse(cc.p(0, 100000))
	sprite:setPhysicsBody(body)
	sprite:move(x, y)
	sprite:setTag(101)
	self:addChild(sprite)
end

return PhysicsScene
